package com.situ.feemanagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.feemanagement.mapper.DepartmentMapper;
import com.situ.feemanagement.pojo.entity.Department;
import com.situ.feemanagement.pojo.query.DepartmentQuery;
import com.situ.feemanagement.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> selectAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public Map<String, Object> selectByPage(DepartmentQuery departmentQuery) {
        PageHelper.startPage(departmentQuery.getPage(), departmentQuery.getLimit());
        List<Department> list = departmentMapper.selectByPage(departmentQuery);
        PageInfo<Department> pageInfo = new PageInfo<>(list);
        int totalCount = (int) pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", totalCount);
        map.put("list", list);
        return map;
    }

    @Override
    public void deleteById(int id) {
        departmentMapper.deleteById(id);
    }

    @Override
    public void deleteByAll(Integer[] ids) {
        for (Integer id : ids) {
            departmentMapper.deleteById(id);
        }
    }

    @Override
    public void add(Department department) {
        departmentMapper.add(department);
    }

    @Override
    public Department selectById(int id) {
        return departmentMapper.selectById(id);
    }

    @Override
    public void update(Department department) {
        departmentMapper.update(department);
    }
}
